package com.qiaodata.datatrans.web;

import com.alibaba.fastjson.JSONObject;
import com.qiaodata.datatrans.model.TransRecord;
import com.qiaodata.datatrans.service.TransRecordService;
import com.qiaodata.datatrans.utils.JsonRet;
import com.qiaodata.datatrans.utils.PostgresqlUtil;
import com.qiaodata.datatrans.utils.StringUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created by Administrator on 2016/11/15 0015.
 */
@RestController
@RequestMapping("/askCompare")
@Slf4j
public class AskController {

    @Autowired
    private TransRecordService transRecordService;

    @ApiOperation("简历查询接口")
    @RequestMapping(value = "/getAskResult", method = {RequestMethod.POST, RequestMethod.GET})
    public Object getAskResult(@ApiParam(required = true, name = "tel_id", value = "手机号码") @RequestParam(value = "tel_id") String tel_id,
                               @ApiParam(required = true, name = "email", value = "电子邮箱") @RequestParam("email") String email,
                               @ApiParam(required = true, name = "refresh_time", value = "最新版本时间戳") @RequestParam(value = "refresh_time") String refresh_time) throws Exception {
        if (tel_id!=null && !"".equals(tel_id.trim())
                && email!=null
                && !"".equals(email.trim())
                && refresh_time!=null
                && !"".equals(refresh_time.trim())) {
            //记录日志
            TransRecord transRecord = new TransRecord();
            String flag = "0";//版本比较结果：0--巧达版本高；1--全职版本高
            log.debug("入参为：tel_id="+tel_id+", email="+email+", refresh_time="+refresh_time);
            //查询postgreSQL库查看是否有，如果有返回一条最新；如果没有，返回空
            JSONObject jsonObject = PostgresqlUtil.getOneNewResumer(tel_id, email);
            //定义返回数据结果
            JSONObject retJson = new JSONObject();
            retJson.put("tel_id", tel_id);
            retJson.put("email", email);
            retJson.put("refresh_time", refresh_time);
            //判断结果
            if (jsonObject==null) {
                //记录查询操作
                transRecord.setMobile(tel_id);
                transRecord.setEmail(email);
                transRecord.setCreateDate(new Date());
                transRecord.setFlag(flag);
                transRecord.setState("0");//表示该记录是ask过程中产生的
                transRecord.setRefreshTimeInQz(StringUtil.parseInt(refresh_time));
                transRecord.setRefreshTimeOutQd(0);
                transRecordService.insert(transRecord);
                //说明巧达不存在该条记录，返回需要入库
                log.debug("该条记录巧达需要入库:tel_id="+tel_id+",email="+email);

                return JsonRet.getInstance("该条记录巧达需要入库", retJson);
            }
            Object refObj = jsonObject.get("refresh_time");
            if (refObj==null || (0l==Long.parseLong(refObj.toString())) || "".equals(refObj.toString())) {
                //记录查询操作
                transRecord.setMobile(tel_id);
                transRecord.setEmail(email);
                transRecord.setCreateDate(new Date());
                transRecord.setFlag(flag);
                transRecord.setState("0");
                transRecord.setRefreshTimeInQz(StringUtil.parseInt(refresh_time));
                transRecord.setRefreshTimeOutQd(0);
                transRecordService.insert(transRecord);
                log.debug("该条记录巧达需要入库:tel_id="+tel_id+",email="+email);

                return JsonRet.getInstance("该条记录巧达需要入库", retJson);
            }
            //巧达数据库的最新记录
            long retime = Long.parseLong(refObj.toString());
            String tel_id_qd = (String) jsonObject.get("tel_id");
            String email_qd = (String) jsonObject.get("email");
            //全职网的最新时间
            long refreshIn = StringUtil.parseLong(refresh_time);
            if (refreshIn>retime) {
                flag = "1";//输入版本高
            }
            if (refreshIn<=retime) {
                flag = "0";//巧达版本高
            }
            //判断结果：含有结果，且巧达比较新
            if ("0".equals(flag)) {
                //记录查询操作
                transRecord.setMobile(tel_id);
                transRecord.setEmail(email);
                transRecord.setCreateDate(new Date());
                transRecord.setFlag(flag);
                transRecord.setState("0");
                transRecord.setRefreshTimeInQz(StringUtil.parseInt(refresh_time));
                transRecord.setRefreshTimeOutQd((int) retime);
                transRecordService.insert(transRecord);
                log.debug("该条记录巧达版本最新，不需要入库:tel_id="+tel_id);

                retJson.put("tel_id", tel_id_qd);
                retJson.put("email", email_qd);
                retJson.put("refresh_time", retime);
                return JsonRet.getInstance("该条记录巧达版本最新，不需要入库，返回data是巧达最新版本数据", retJson);
            }
            //判断结果：含有结果，且巧达比较旧
            if("1".equals(flag)) {
                //记录查询操作
                transRecord.setMobile(tel_id);
                transRecord.setEmail(email);
                transRecord.setCreateDate(new Date());
                transRecord.setFlag(flag);
                transRecord.setState("0");
                transRecord.setRefreshTimeInQz(StringUtil.parseInt(refresh_time));
                transRecord.setRefreshTimeOutQd((int) retime);
                transRecordService.insert(transRecord);
                log.debug("该条记录巧达版本较旧，需要入库:tel_id="+tel_id+",email="+email);

                return JsonRet.getInstance("该条记录巧达需要入库", retJson);
            }

            //记录查询操作
            transRecord.setMobile(tel_id);
            transRecord.setEmail(email);
            transRecord.setCreateDate(new Date());
            transRecord.setFlag(flag);
            transRecord.setState("0");
            transRecord.setRefreshTimeInQz(StringUtil.parseInt(refresh_time));
            transRecord.setRefreshTimeOutQd((int) retime);
            transRecordService.insert(transRecord);

            return JsonRet.getInstance("该条记录巧达需要入库", retJson);
        }

        return JsonRet.getInstance("输入参数为空，请检查输入参数", null);
    }

}
